package com.acmen.easyapi.web;


import com.acmen.easyapi.dto.PressureTestCmd;
import com.acmen.easyapi.model.Response;
import com.acmen.easyapi.service.PressureTestService;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author acmen
 * 组合测试服务
 * @date 2022-03-16
 */
@RestController
public class PressureTestController {
    @Autowired
    private PressureTestService pressureTestService;
    /**
     * 组合测试页面
     * @param request
     * @param model
     * @return
     */
    @GetMapping(value = {"/test/pressure-test"})
    public ModelAndView pressureTest(HttpServletRequest request, ModelMap model) {
        return pressureTestService.index(request,model);
    }


    /**
     * 开始组合测试
     * @param pressureTestCmd
     * @param response
     * @throws InterruptedException
     * @throws IOException
     */
    @PostMapping(value = "/test/pressure-test/exec")
    public void exec(@RequestBody PressureTestCmd pressureTestCmd, HttpServletResponse response) throws InterruptedException, IOException {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json; charset=utf-8");
        pressureTestService.exec(pressureTestCmd,response);
    }

    /**
     * 测试进程
     * @param request
     * @param response
     * @throws IOException
     */
    @GetMapping(value = "/test/pressure-test/progress")
    public void progress(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("application/json; charset=utf-8");
        pressureTestService.progress(request,response);
    }

    @GetMapping(value = "/test/pressure-tests/{appName}")
    public void getPressureTests(@PathVariable String appName,HttpServletResponse response) throws IOException {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json; charset=utf-8");
        response.getWriter().println(JSON.toJSONString(Response.of(pressureTestService.selectListByAppName(appName))));
    }

    @DeleteMapping(value = "/test/pressure-test/{id}")
    public void deletePressureTests(@PathVariable  String id, HttpServletResponse response)  throws IOException{
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json; charset=utf-8");
        response.getWriter().println(JSON.toJSONString(Response.of(pressureTestService.delete(id))));
    }
}
